import {
    createSwitchNavigator,
    NavigationActions
} from 'react-navigation';
import React, {Component} from 'react';

import Printer from './printer/Printer';
import Test from './test/index'

const Root = createSwitchNavigator({
    printer:Printer,
    stest:Test
},{
    headerMode:"none"
})

class SettingRoot extends Component {
    constructor(props){
        super(props);
        this.state = {
            routeName:props.routeName || "printer"
        }
    }
    render() {
        return (
            <Root ref = {(ref)=>{
                if(ref && !this._dispatch){
                    this._dispatch = ref.dispatch;
                }
            }} />
        );
    }
    componentWillReceiveProps(next){
        if(next && next.routeName != this.state.routeName){
            this.setState({
                routeName:next.routeName
            })
        }
        this.changeTab(next.routeName);
    }
    changeTab(tabName){
        if(typeof this._dispatch == 'function'){
            const navigateAction = NavigationActions.navigate({
                routeName: tabName,
                params: {},
                action: NavigationActions.navigate({ routeName: tabName}),
            });
            this._dispatch(navigateAction);
        }
    }
}

export default SettingRoot;